package com.example.fastshare;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.example.fastshare.alljoyn.MainApp;
import com.example.fastshare.alljoyn.Utilities;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.QuickContactBadge;
import android.widget.Toast;

public class ProfileActivity extends FragmentActivity {

	private EditText userName;
	private EditText macAddress;
	private ImageButton backBtn;
	private QuickContactBadge profilePhoto;
	private QuickContactBadge icon1;
	private QuickContactBadge icon2;
	private QuickContactBadge icon3;
	private String checkedAvatar;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_profile);

		profilePhoto = (QuickContactBadge) findViewById(R.id.activity_profile_quickContactBadge);
		icon1 = (QuickContactBadge) findViewById(R.id.activity_profile_quickContactBadge1);
		icon2 = (QuickContactBadge) findViewById(R.id.activity_profile_quickContactBadge2);
		icon3 = (QuickContactBadge) findViewById(R.id.activity_profile_quickContactBadge3);

		icon1.setOnClickListener( new OnClickListener() {			
			@Override
			public void onClick(View v) {
				profilePhoto.setImageResource(R.drawable.a);
				checkedAvatar = "a";
			}
		});

		icon2.setOnClickListener( new OnClickListener() {			
			@Override
			public void onClick(View v) {
				profilePhoto.setImageResource(R.drawable.b);
				checkedAvatar = "b";
			}
		});

		icon3.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View v) {
				profilePhoto.setImageResource(R.drawable.c);
				checkedAvatar = "c";
			}
		});

		userName = (EditText) findViewById(R.id.activity_profile_UserName_editText);
		macAddress = (EditText) findViewById(R.id.activity_profile_DeviceName_editText);
		backBtn = (ImageButton) findViewById(R.id.activity_profile_Title_Button);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		
		LoadInfo();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LoadInfo();
	}

	private void LoadInfo() {
		if (MainApp.m_userInfo != null) {
			userName.setText(MainApp.m_userInfo.getM_userName());
		} else {
			userName.setText("You Don't Have Any Name");
		}
		if (MainApp.m_userInfo != null) {
			macAddress.setText(MainApp.m_userInfo.getM_macAddress());
		} else {
			WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
			WifiInfo wInfo = wifiManager.getConnectionInfo();
			String _macAddress = wInfo.getMacAddress();
			macAddress.setText(_macAddress);
		}
		if (MainApp.m_userInfo != null) {
			if (MainApp.m_userInfo.getM_avatar().equals("1")) {
				profilePhoto.setImageResource(R.drawable.a);
			}
			if (MainApp.m_userInfo.getM_avatar().equals("2")) {
				profilePhoto.setImageResource(R.drawable.b);
			}
			if (MainApp.m_userInfo.getM_avatar().equals("3")) {
				profilePhoto.setImageResource(R.drawable.c);
			}

		} else {
//			profilePhoto.setImageResource(R.drawable.c);
		}

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
//		if (checkedAvatar == "a") {
//			Utilities.saveProfile(this, userName.getText().toString(), "1");
//		} else {
//			if (checkedAvatar == "b") {
//				Utilities.saveProfile(this, userName.getText().toString(), "2");
//			} else {
//				Utilities.saveProfile(this, userName.getText().toString(), "3");
//			}
//		}
		
		if (checkedAvatar == "a") {
			Utilities.saveProfile(this, userName.getText().toString(), "1");
		} 
		if (checkedAvatar == "b") {
			Utilities.saveProfile(this, userName.getText().toString(), "2");
		}
		if (checkedAvatar == "c") {
			Utilities.saveProfile(this, userName.getText().toString(), "3");
		}
		MainApp.m_userInfo = Utilities.loadProfile(this);

	}

}
